package liumengbaio.lolzhangyoubao.Activity.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.io.IOException;
import java.util.List;

import liumengbaio.lolzhangyoubao.Activity.Activity.HeroDetailsActivity;
import liumengbaio.lolzhangyoubao.Activity.Adapter.FreeHeroAdapter;
import liumengbaio.lolzhangyoubao.Activity.Adapter.NextFreeHeroAdapter;
import liumengbaio.lolzhangyoubao.Activity.InFo.FreeHeroInfo;
import liumengbaio.lolzhangyoubao.Activity.InFo.NextFreeHeroInfo;
import liumengbaio.lolzhangyoubao.Activity.InFo.WeekFree;
import liumengbaio.lolzhangyoubao.Activity.View.ScrollGridView;
import liumengbaio.lolzhangyoubao.Activity.utils.AssetsUtils;
import liumengbaio.lolzhangyoubao.Activity.utils.NextAssetsUtils;
import liumengbaio.lolzhangyoubao.R;

/**
 * 周免
 * Created by lanouhn on 16/8/24.
 */
public class FragmentHeroWeekFree extends Fragment {
    private TextView thisweekfree, thisdate, nextweekfree, nextdate;
    private WeekFree weekfree;
    String url = "http://lol.zhangyoubao.com/apis/rest/RolesService/freeV1?i_=o0OBghYqiNtFOBcGWwxN9X0mpev90CpqrOSB7/Xyhjw=&deviceId=o0OBghYqiNtFOBcGWwxN9X0mpev90CpqrOSB7/Xyhjw=&t_=1472031793319&p_=12302&v_=400901&a_=lol&pkg_=com.anzogame.lol&d_=android&osv_=17&cha=baiduMartket&u_=15863305&modle_=vivo%20Y15T&api=RolesService.freeV1&time=1472031793&game=lol&secretId=AKIDz8krbsJ5yKBZQpn74WFkmLPx3gnPhESA&secretVersion=v1.0&nonce=955723&secretSignature=+PVLFiOKrJMzjAOt0nq0o2J7jds=";


    /**
     * 本周免费英雄实体类集合
     */
    private List<FreeHeroInfo> freeHeroInfos;
    private List<NextFreeHeroInfo> nextFreeHeroInfos;

    private FreeHeroAdapter heroAdapter;
    private NextFreeHeroAdapter nextFreeHeroAdapter;

    private ScrollGridView gridView;
    private ScrollGridView gridViewnext;
    private ScrollView scrollView;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_weekfree, null);
        thisweekfree = (TextView) view.findViewById(R.id.thisweekfree);
        thisdate = (TextView) view.findViewById(R.id.thisdate);
        nextweekfree = (TextView) view.findViewById(R.id.nextweekfree);
        nextdate = (TextView) view.findViewById(R.id.nextdate);
        gridView = (ScrollGridView) view.findViewById(R.id.gridview);
        gridViewnext = (ScrollGridView) view.findViewById(R.id.gridviewnext);
        scrollView = (ScrollView) view.findViewById(R.id.scrollView);
        getDataFromWeb();

        return view;

    }


    private void getDataFromWeb() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder()
                        .url(url).build();
                Call call = client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Request request, IOException e) {
                    }

                    @Override
                    public void onResponse(Response response) throws IOException {
                        String result = response.body().string();
                        Gson gson = new Gson();
                        weekfree = gson.fromJson(result, WeekFree.class);
                        if (weekfree.getData().getCurrentperiod().size() > 0) {
                            freeHeroInfos = AssetsUtils.getFreeHerosList(getActivity(), weekfree.getData().getCurrentperiod());
                            if (weekfree.getData().getNextperiod() != null) {
                                nextFreeHeroInfos = NextAssetsUtils.getNextFreeHeroList(getActivity(), weekfree.getData().getNextperiod());
                            } else {

                            }

                            handler.sendEmptyMessage(1);
                        } else {
                            handler.sendEmptyMessage(0);
                        }


                    }
                });

            }
        }).start();

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                FreeHeroInfo info = freeHeroInfos.get(position);

                Intent intent = new Intent(getActivity(), HeroDetailsActivity.class);
                intent.putExtra("hero", info);
                startActivity(intent);


            }
        });
    }

    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    Toast.makeText(getActivity(), "抱歉,没数据", Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    thisweekfree.setText(weekfree.getData().getCurrenttitle());
                    thisdate.setText("(" + weekfree.getData().getCurrentdate() + ")");
                    nextweekfree.setText(weekfree.getData().getNexttitle());
                    heroAdapter = new FreeHeroAdapter(freeHeroInfos, getActivity());
                    gridView.setAdapter(heroAdapter);
                    if (weekfree.getData().getNextperiod() != null) {
                        nextdate.setText("(" + weekfree.getData().getNextdate() + ")");
                        nextFreeHeroAdapter = new NextFreeHeroAdapter(nextFreeHeroInfos, getActivity());
                        gridViewnext.setAdapter(nextFreeHeroAdapter);
                    }

                    scrollView.smoothScrollTo(0, 0);


                    break;
            }
        }
    };


}
